package br.com.veterinariavillage.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import br.com.veterinariavillage.model.Veterinario;

public class DAOVeterinario implements IDAOVeterinario {

	Connection conexao = GenericDao.getConnection();

	@Override
	public ArrayList<Veterinario> localizar() {
		ArrayList<Veterinario> listaVeterinario = new ArrayList<Veterinario>();
		ResultSet rs = null;
		try{
			PreparedStatement ps = conexao.prepareStatement("select cod_veterinario, nome, numcrmv, email, telefone from Veterinario ");
			ps.execute();
			rs = ps.getResultSet();
			while (rs.next()){
				Veterinario veterinario = new Veterinario();
				veterinario.setCodigo(rs.getInt("cod_veterinario"));
				veterinario.setNome(rs.getString("nome"));
				veterinario.setEmail(rs.getString("email"));
				veterinario.setCrmv(rs.getString("crmv"));
				veterinario.setTelefone(rs.getString("telefone"));
				listaVeterinario.add(veterinario);
			}
			ps.close();
		}catch (Exception e){
			e.printStackTrace();
		}
		return listaVeterinario;
	}

	@Override
	public ArrayList<Veterinario> localizarPorNome(String nome) {
		ArrayList<Veterinario> listaVeterinario = new ArrayList<Veterinario>();
		ResultSet rs = null;
		try{
			PreparedStatement ps = conexao.prepareStatement(" select cod_veterinario, nome, numcrmv, email, telefone " +
															" from Veterinario where UPPER(nome) like UPPER(?+'%') ");
			ps.setString(1, nome);
			ps.execute();
			rs = ps.getResultSet();
			while (rs.next()){
				Veterinario veterinario = new Veterinario();
				veterinario.setCodigo(rs.getInt("cod_veterinario"));
				veterinario.setNome(rs.getString("nome"));
				veterinario.setEmail(rs.getString("email"));
				veterinario.setCrmv(rs.getString("crmv"));
				veterinario.setTelefone(rs.getString("telefone"));
				listaVeterinario.add(veterinario);
			}
			ps.close();
		}catch (Exception e){
			e.printStackTrace();
		}
		return listaVeterinario;
	}

	@Override
	public Veterinario localizarPorCrmv(String crmv) {
		Veterinario veterinario = null;
		ResultSet rs = null;
		try{
			PreparedStatement ps = conexao.prepareStatement(" select cod_veterinario, nome, numcrmv, email, telefone, datanasc " +
															" from Veterinario where crmv = ? ");
			ps.setString(1, crmv);
			ps.execute();
			rs = ps.getResultSet();
			while (rs.next()){
				veterinario = new Veterinario();
				veterinario.setCodigo(rs.getInt("cod_veterinario"));
				veterinario.setNome(rs.getString("nome"));
				veterinario.setEmail(rs.getString("email"));
				veterinario.setCrmv(rs.getString("crmv"));
				veterinario.setTelefone(rs.getString("telefone"));
			}
			ps.close();
		}catch (Exception e){
			e.printStackTrace();
		}
		
		return veterinario;
	}

	@Override
	public void inserir(Veterinario c) {
		try{
			PreparedStatement ps = conexao.prepareStatement("insert into Veterinario(nome, numcrmv, email, telefone) values (?,?,?,?)");
			ps.setString(1, c.getNome());
			ps.setString(2, c.getCrmv());
			ps.setString(3, c.getEmail());
			ps.setString(4, c.getTelefone());
			ps.execute();
			ps.close();
		}catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void atualizar(Veterinario c) {
		try{
			PreparedStatement ps = conexao.prepareStatement(" update Veterinario set nome = ?, numcrmv = ?, email = ?, telefone = ? " +
															" where cod_veterinario = ?");
			ps.setString(1, c.getNome());
			ps.setString(2, c.getCrmv());
			ps.setString(3, c.getEmail());
			ps.setString(5, c.getTelefone());
			ps.setInt(6, c.getCodigo());
			ps.execute();
			ps.close();
		} catch(Exception e){
			e.printStackTrace();
		}
	}

	@Override
	public void excluir(Veterinario c) {
		try{
			PreparedStatement ps = conexao.prepareStatement("delete Veterinario where cod_veterinario = ?");
			ps.setInt(1,c.getCodigo());
			ps.execute();
			ps.close();
		}catch(Exception e){
			e.printStackTrace();
		}
	}

	@Override
	public Veterinario localizar(int codigo) {
		// TODO Auto-generated method stub
		return null;
	}


}
